package com.telchina.common.service.impl;

import com.telchina.common.core.domain.AjaxResult;
import com.telchina.common.service.IBaseFlowService;
import com.telchina.workorder.third.service.BpmTaskThirdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class BaseFlowServiceImpl implements IBaseFlowService {


    @Autowired
    private BpmTaskThirdService taskThirdService;

    /**
     * 验证任务是否属于登录用户
     *
     * @param taskId
     */
    @Override
    public boolean checkUserIdDoTask(String taskId) {
        // 验证远程任务是否可以处理
        AjaxResult checkUserIdDoTaskResult = taskThirdService.checkUserIdDoTask(taskId);
        if (!Objects.equals(checkUserIdDoTaskResult.get(AjaxResult.CODE_TAG), 0)) {
            throw new RuntimeException("系统异常,验证任务错误：" + checkUserIdDoTaskResult.get(AjaxResult.MSG_TAG));
        }
        if (!Objects.equals(checkUserIdDoTaskResult.get(AjaxResult.DATA_TAG), true)) {
            return false;
        }
        return true;
    }

    /**
     * @param processInstanceId
     * @return
     */
    @Override
    public AjaxResult getOptButton(String taskDefinitionkey) {
        AjaxResult nextFlowResult = taskThirdService.getButton(taskDefinitionkey);
        if (!Objects.equals(nextFlowResult.get(AjaxResult.CODE_TAG), 0)) {
            throw new RuntimeException("系统异常,验证任务错误：" + nextFlowResult.get(AjaxResult.MSG_TAG));
        }
        return AjaxResult.success(nextFlowResult.get(AjaxResult.DATA_TAG));
    }
}
